package cloudoer.blog.controller;

import cloudoer.blog.base.BaseController;
import cloudoer.blog.entity.User;
import cloudoer.blog.util.VerificationCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/visitor")
public class VisitorController extends BaseController {

    /*@RequestMapping("main")
    public String main(){
        return "visitor/main";
    }*/
    @RequestMapping("loginUI")
    public String loginUI(){
        return "visitor/login";
    }
    @RequestMapping("registerUI")
    public String registerUI(){
        return "visitor/register";
    }
    @RequestMapping("validationUI")
    public String validationUI(){
        return "visitor/validation";
    }

    @RequestMapping("/index")
    public String index(){
        return "visitor/index";
    }

    @RequestMapping("/east")
    public String east(){
        return "visitor/east";
    }

    @RequestMapping("/article")
    public String article(){
        return "visitor/article";
    }

    @RequestMapping("/login")
    public Map<String, Object> login(User user, String verification, HttpServletRequest request){
        String code = ((String) request.getSession().getAttribute("code")).toLowerCase();
        verification = verification.toLowerCase();
        if (!code.equals(verification)){
            return error("登录失败,看清楚验证码哦");
        }
        User u = userService.login(user);
        System.out.println(u);
        if (u == null){
            return error("登录失败，看清楚登录信息哦");
        }else {
            request.getSession().setAttribute("visitor", u);
            return success("登录成功");
        }
    }

    @RequestMapping("/register")
    public Map<String, Object> register(User user, String password0) throws ServletException {
        if (password0 != null || password0.trim().equals(user.getPassword().trim())){
            userService.add(user);
            emailService.sendMail(user.getEmail(), user.getId());
            return success("我给你发了一封特殊的邮件，快去看看吧！");
        }else {
            return error("两次密码都不对哎！");
        }
    }

    @RequestMapping("/validation")
    public Map<String, Object> validation (User user, HttpServletRequest request){
        User u = userService.validation(user);
        if (u != null){
            request.getSession().setAttribute("visitor", u);
            return success("验证成功");
        }else {
            return error("有毒吧，竟然没有验证通过，去加站长QQ怼死他");
        }
    }

    @RequestMapping("/verificationCode")
    @ResponseBody
    public String verificationCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置响应的类型格式为图片格式
        /*response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        Map<String, Object> map = VerificationCode.newVerificationCode(150,30);
        String code = (String) map.get("code");
        request.getSession().setAttribute("code", code);
        ImageIO.write((BufferedImage) map.get("image"),"jpg",response.getOutputStream());
        response.getOutputStream().flush();
        response.getOutputStream().close();*/
        // randomCode记录随机产生的验证码
        StringBuffer randomCode = new StringBuffer();
        Random random = new Random();
        // 随机产生codeCount个字符的验证码。
        for (int i = 0; i < 4; i++) {
            String strRand = String.valueOf(VerificationCode.codeSequence[random.nextInt(VerificationCode.codeSequence.length)]);
            // 将产生的四个随机数组合在一起。
            randomCode.append(strRand);
        }
        String code = randomCode.toString();
        request.getSession().setAttribute("code", code);
        return code;
    }

}
